using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class CrushWall:MonoBehaviour {
	new Rigidbody2D rigidbody;
	new Collider2D collider;
	Mob mob;

	[SerializeField] float damageInterval;
	[SerializeField] float damageAmount;

	void Start() {
		rigidbody=GetComponent<Rigidbody2D>();
		collider=GetComponent<Collider2D>();
		mob=GetComponent<Mob>();
	}

	float timeAfterDamage;
	void FixedUpdate() {
		timeAfterDamage+=Time.deltaTime;
		if(timeAfterDamage>damageInterval) {
			UpdateDamage();
			timeAfterDamage-=damageInterval;
		}

		void UpdateDamage() {
			Vector2 velocity = mob.targetVelocity;
			if(velocity.sqrMagnitude==0) return;
			Bounds targetBound = collider.bounds;
			targetBound.min+=(Vector3)velocity*Time.deltaTime;
			targetBound.max+=(Vector3)velocity*Time.deltaTime;
			DamageStats damage = new DamageStats(damageAmount);

			for(int x = (int)targetBound.min.x;x<targetBound.max.x;x++) {
				for(int y = (int)targetBound.min.y;y<targetBound.max.y;y++) {
					TilemapController.DamageTile(new Vector2Int(x,y),damage);
				}
			}
		}
	}
}
